package com.example.hasee.zhcd.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.NetUtil.HttpUtils;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.timebutton.TimeButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by hasee on 2018/8/7.
 */

public class BindActivity extends Activity implements View.OnClickListener {
    public HttpUtils httpUtils;
    private TextView title;
    private ImageView back;
    private ImageView home;
    private EditText phonenumber;
    private EditText vercode;
    private Button btn;
    private TimeButton btn_code;
    private String phonenumberString;
    private String phoneidString;
    private String vercodeString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        EventBus.getDefault().register(this); //注册EventBus
        httpUtils = HttpUtils.getInstance(this);
        init();
        phonenumberString=getIntent().getExtras().getString("phone");
        phonenumber.setText(phonenumberString);
        btn_code = (TimeButton) findViewById(R.id.btn_code);
        btn_code.onCreate(savedInstanceState);
        title.setText("绑定账号");
        back.setOnClickListener(this);
        home.setVisibility(View.GONE);
        btn.setOnClickListener(this);
        btn_code.setOnClickListener(this);
    }


    public void init() {
        TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        phoneidString = tm.getDeviceId();
        phonenumberString = getIntent().getExtras().getString("phone");
        title = (TextView) findViewById(R.id.centertitle);
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        phonenumber = (EditText) findViewById(R.id.mobilephone);
        vercode = (EditText) findViewById(R.id.code);
        btn = (Button) findViewById(R.id.btn);
    }

    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZH_Binding":
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode) || "2".equals(rspcode)) {
                        Toast.makeText(getApplicationContext(), rspmessage, Toast.LENGTH_SHORT).show();
                    } else if ("1".equals(rspcode)) {
                        finishdialog(rspmessage);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case "getVerCode":
                Toast.makeText(getApplicationContext(), "您已成功获取验证码，发送可能会有延迟，请耐心等待，请勿多次重复获取！",
                        Toast.LENGTH_SHORT).show();
                break;
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventMsg msg) {
        getEventData(msg);
    }

    private void getBind() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPhone", phonenumberString);
        map.put("strPhoneID", phoneidString);
        httpUtils.get("ZH_Binding", map, "ZH_Binding");
    }

    private void getVerCode() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPhone", phonenumberString);
        httpUtils.get("getVerCode", map, "getVerCode");
    }

    private View inputCheck() {
        if (phonenumberString.length() == 0) {
            Toast.makeText(BindActivity.this, "请输入手机号码！", Toast.LENGTH_LONG).show();
            return phonenumber;
        } else if (vercodeString.length() == 0) {
            Toast.makeText(BindActivity.this, "请输入验证码！", Toast.LENGTH_LONG).show();
            return vercode;
        }
        return null;
    }

    private void finishdialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(BindActivity.this);
        builder.setMessage(message);
        builder.setTitle("提示");
        builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                arg0.dismiss();
                finish();
            }
        });
        builder.create().show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
            case R.id.btn:
                phonenumberString = phonenumber.getText().toString().trim();
                vercodeString = vercode.getText().toString().trim();
                View errorview = inputCheck();
                if (errorview != null) {
                    errorview.setFocusable(true);
                    errorview.requestFocus();
                    errorview.setFocusableInTouchMode(true);
                    return;
                } else {
                    getBind();
                }
                break;
            case R.id.btn_code:
                phonenumberString = phonenumber.getText().toString().trim();
                if ("".equals(phonenumberString)) {
                    btn_code.setTextAfter("秒后获取").setTextBefore("发送验证码").setLenght(0 * 1000);
                    Toast.makeText(BindActivity.this, "请输入手机号码！", Toast.LENGTH_LONG).show();
                } else {
                    btn_code.setTextAfter("秒后获取").setTextBefore("发送验证码").setLenght(60 * 1000);
                    getVerCode();
                }
                break;
        }
    }
}
